package com.req.vmsg.util;

import java.util.ArrayList;
import java.util.List;

import com.util.vmsg.beans.MobileNumber;
import com.util.vmsg.beans.TxtWebMessage;
import com.util.vmsg.beans.Vehicle;
import com.vutil.src.dao.VMsgDAO;

public class VMsgUtil {

	/** 
	 * This method validates request data by Checking for minimum 2 tokens
	 If request size is less than 2 ==> send help content
		 If token 1 
			 is "Register / DeRegister" ==> call the business method
			 is NOT "Register / DeRegister"  
				 Check for existing vehicle number 
					if we can find an existing vehicle number ==> relay message
					if not, send help content 
	*/
	public static TxtWebMessage validateRequestData(TxtWebMessage txtWebMessage) {
		String txtMsg = txtWebMessage.getTxtWebMessage();
		
		if(txtMsg!=null)
		{
			txtMsg = txtMsg.trim();
			if(!txtMsg.isEmpty())
			{
				System.out.println("txtMsg = " + txtMsg);
				
				String[] msgTokens = txtMsg.split(TxtWebConstants.Grammer.SPACE);
				
				if(msgTokens.length == 1 && msgTokens[0].equalsIgnoreCase(TxtWebConstants.RequestTypes.HELP))
				{
					txtWebMessage.setRequestType(TxtWebConstants.RequestTypes.HELP);
					txtWebMessage.setHelpMessage(TxtWebConstants.VMsgHelpMessages.KEY_WORD_HELP);
					txtWebMessage.setDataValid(true);
				}
				
				if(msgTokens.length == 2 && msgTokens[0].equalsIgnoreCase(TxtWebConstants.RequestTypes.HELP))
				{
					if(msgTokens[1].equalsIgnoreCase(TxtWebConstants.VMsgHelpMessages.REGISTER))
					{
						txtWebMessage.setRequestType(TxtWebConstants.RequestTypes.HELP);
						txtWebMessage.setHelpMessage(TxtWebConstants.VMsgHelpMessages.REGISTER_HELP);
						txtWebMessage.setDataValid(true);
					}
					else if (msgTokens[1].equalsIgnoreCase(TxtWebConstants.VMsgHelpMessages.DEREGISTER))
					{
						txtWebMessage.setRequestType(TxtWebConstants.RequestTypes.HELP);
						txtWebMessage.setHelpMessage(TxtWebConstants.VMsgHelpMessages.DEREGISTER_HELP);
						txtWebMessage.setDataValid(true);
					}
				}
					
				if(msgTokens.length >= 2)
				{
					if(msgTokens[0].equalsIgnoreCase(TxtWebConstants.Grammer.REGISTER))
					{
						txtWebMessage.setRequestType(TxtWebConstants.RequestTypes.REGISTER);
						txtWebMessage.setDataValid(true);
					}
					else if(msgTokens[0].equalsIgnoreCase(TxtWebConstants.Grammer.DEREGISTER))
					{
						txtWebMessage.setRequestType(TxtWebConstants.RequestTypes.DEREGISTER);
						txtWebMessage.setDataValid(true);
					}
					else
					{
						boolean isExistingHandle = isExistingHandle(msgTokens[0],TxtWebConstants.HandleTypes.VEHICLE);
						if(isExistingHandle)
						{
							txtWebMessage.setDataValid(isExistingHandle);
							txtWebMessage.setRequestType(TxtWebConstants.RequestTypes.RELAY_MESSAGE);
						}
						
					}
				}
			}
		}
		
		return txtWebMessage;
	}

	private static boolean isExistingHandle(String handleId, String handleType) {
		// TODO Write code to check for an existing handle
		if(handleType.equals(TxtWebConstants.HandleTypes.VEHICLE))
		{
			VMsgDAO vMsgDAO = VMsgDAO.getInstance();
			Vehicle vehicle = new Vehicle(handleId);
			boolean isExistingVehicle = vMsgDAO.isExistingVehicleNumber(vehicle);
			return isExistingVehicle;
		}
		else if (handleType.equals(TxtWebConstants.HandleTypes.MOBILENUMBER))
		{
			VMsgDAO vMsgDAO = VMsgDAO.getInstance();
			MobileNumber mobileNumber = new MobileNumber(handleId, false, null);
			boolean isExistingMobile = vMsgDAO.isExistingMobileNumber(mobileNumber);
			return isExistingMobile;
		}
		return false;
	}

}
